package com.linkai.controller.admin.business;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linkai.controller.admin.BaseController;
import com.linkai.dto.admin.business.*;
import com.linkai.entity.Bproduct;
import com.linkai.enums.ResultDTO;
import com.linkai.service.BproductService;
import com.linkai.service.InputorderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yamon
 * @since 2021-01-09
 */
@Controller
@RequestMapping("/admin/business/inputOrder")
public class InputOrderController extends BaseController {
    @Autowired
    private InputorderService inputorderService;
    @Autowired
    private BproductService bproductService;

    @GetMapping("/add")
    public ModelAndView add() {
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/business/inputOrder/add");
        return modelAndView;
    }


    @PostMapping("/save")
    @ResponseBody
    public ResultDTO<Integer> saveOneInputOrder(@RequestBody String inputOrder) {
        JSONObject jsonObject = JSONObject.parseObject(inputOrder);
        final JSONArray jsonArray = jsonObject.getJSONArray("products");
        final List<InputOrder4JsonDto> products = jsonArray.toJavaList(InputOrder4JsonDto.class);
        String orderId = (String) JSON.parseObject(inputOrder).get("orderId");
        String supplier = (String) JSON.parseObject(inputOrder).get("supplier");
        String employeeId = (String) JSON.parseObject(inputOrder).get("employeeId");
        String accountMoeny = (String) JSON.parseObject(inputOrder).get("accountMoeny");
        String storeId = (String) JSON.parseObject(inputOrder).get("storeId");
        String price = (String) JSON.parseObject(inputOrder).get("price");
        String back = (String) JSON.parseObject(inputOrder).get("back");
        final String inputDate = (String) JSON.parseObject(inputOrder).get("inputDate");
        BigDecimal moeny = new BigDecimal(price);
        InputOrderJsonDto inputOrderJsonDto = new InputOrderJsonDto();
        inputOrderJsonDto.setOrderId(orderId);
        inputOrderJsonDto.setSupplier(Integer.valueOf(supplier));
        inputOrderJsonDto.setEmployeeId(Integer.valueOf(employeeId));
        inputOrderJsonDto.setAccountMoeny(Integer.valueOf(accountMoeny));
        inputOrderJsonDto.setStoreId(Integer.valueOf(storeId));
        inputOrderJsonDto.setPrice(moeny);
        inputOrderJsonDto.setBack(back);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            inputOrderJsonDto.setInputDate(sf.parse(inputDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        inputOrderJsonDto.setProductIds(products);
        return super.requestInfo(inputorderService.saveInputOrder(inputOrderJsonDto));
    }

    @GetMapping("/index")
    public ModelAndView index() {
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("inputOrders",inputorderService.listAll());
        modelAndView.setViewName("admin/business/inputOrder/index");
        return modelAndView;
    }

    /**
     * 查找出所有的商品信息
     *
     * @return list
     */
    @RequestMapping(value = "/product/listAll", method = RequestMethod.GET)
    @ResponseBody
    public List<ProductDto> getAllProduct() {
        final List<Bproduct> list = bproductService.list();
        List<ProductDto> productDtos = new ArrayList<>();
        list.forEach(bproduct -> {
            final ProductDto target = new ProductDto();
            BeanUtils.copyProperties(bproduct, target);
            productDtos.add(target);
        });
        return productDtos;
    }

    /**
     * @param selects 指定id
     * @return list
     */
    @RequestMapping(value = "/show2Table", method = RequestMethod.POST)
    @ResponseBody
    public List<InputOrderShowDto> showSelectedProduct(@RequestBody String selects) {
        List<InputOrderShowDto> inputNumberDtos = JSONArray.parseArray(selects, InputOrderShowDto.class);
        final List<InputOrderShowDto> inputOrderShowDtos = inputNumberDtos.stream().map(inputNumberDto -> {
            final Bproduct byId = bproductService.getById(inputNumberDto.getBproId());
            if (byId!=null) {
                byId.setBproNum(inputNumberDto.getBproNum());
                //设置到显示的dto
                final InputOrderShowDto target = new InputOrderShowDto();
                target.setBproNum(inputNumberDto.getBproNum());
                target.setBproId(byId.getBproId());
                target.setBproTitle(byId.getBproTitle());
                target.setBinputPrice(inputNumberDto.getBinputPrice());
                target.setBproUnit(byId.getBproUnit());
                target.setEachSum(BigDecimal.valueOf(target.getBproNum()).multiply(target.getBinputPrice()));
                return target;
            } else {
                return null;
            }
        }).collect(Collectors.toList());
        return inputOrderShowDtos;
    }

    /**
     * 跳转页面
     * @param id id
     * @return 模型
     */
    @RequestMapping(value = "/print/{id}",method = RequestMethod.GET)
    public ModelAndView print(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView();
        //查出来商品和进货单信息
        InputOrderUpdateDto inputOrderUpdateDto = inputorderService.selectInputOrderDto4UpdateById(id);
        modelAndView.addObject("inputOrder",inputOrderUpdateDto);
        modelAndView.setViewName("admin/business/inputOrder/print/print");
        return modelAndView;
    }

    /**
     * 打印页面
     * @param id 进货单id
     * @return 页面
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.GET)
    public ModelAndView update2Page(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView();
        //查出来商品和进货单信息
        InputOrderUpdateDto inputOrderUpdateDto = inputorderService.selectInputOrderDto4UpdateById(id);
        modelAndView.addObject("inputOrder",inputOrderUpdateDto);
        modelAndView.setViewName("admin/business/inputOrder/update/update");
        return modelAndView;
    }
    /**
     * 根据id删除所有进货单信息
     * @param id id
     * @return flag
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultDTO<Boolean> deleteOneEmployee(@PathVariable Integer id){
        return super.successDeleteInfo(inputorderService.removeInputOrderById(id));
    }

}

